package com.hk.service.impl;

import com.hk.dao.ContactUsDao;
import com.hk.model.ContactUs;
import com.hk.model.Document;
import com.hk.service.IContactUsService;
import com.hk.utils.Io;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/20 17:39
 * State：联系我们业务逻辑层
 */
public class ContactUsServiceImpl implements IContactUsService {

    @Override
    public ContactUs findAllContactUs(String path) {
        ContactUsDao contactUsDao = new ContactUsDao();
        ContactUs contactUs = contactUsDao.findAllContactUs();
        //      1、存储路径  2、文件夹 3、文件
        Document document = new Document(path,"ContactUs", "ContactUs.txt");
//        检索存储路径和文件夹或文件 并写入书局
        List<ContactUs> contactUses = new ArrayList<>(5);
        contactUses.add(contactUs);
        writeData(contactUses,document);
//        读取数据
        contactUses = (List<ContactUs>) readData(document);
        contactUs = contactUses.get(0);
        return contactUs;
    }

    /**
     * 读取数据
     * @param document
     * @return
     */
    @Override
    public List<?> readData(Document document) {
        //    根据该对象内多少属性进行转化划分
        final int count = 6;
        List<ContactUs> contactUses = new ArrayList<>(5);
        List<String> list = Io.readData(document);
        for (int i = 0; i < list.size(); i=i+count) {
            ContactUs contactUs = new ContactUs(
                    Integer.parseInt(list.get(i)),
                    list.get(i+1),
                    list.get(i+2),
                    list.get(i+3),
                    list.get(i+4),
                    list.get(i+5)
            );
            contactUses.add(contactUs);
        }
        return contactUses;
    }

    /**
     * 写入书局
     * @param list
     * @param document
     */
    @Override
    public void writeData(List<?> list, Document document) {
        List<ContactUs> contactUses = (List<ContactUs>) list;
        List<String> list1 = new ArrayList<>();
        for (ContactUs contactUs : contactUses) {
            list1.add(String.valueOf(contactUs.getUsId()));
            list1.add(contactUs.getUsSite());
            list1.add(contactUs.getUsPhone());
            list1.add(contactUs.getUsFax());
            list1.add(contactUs.getUsEmail());
            list1.add(contactUs.getUsUrl());
        }
        Io.writeData(list1,document);
    }
}
